// pages/page/pic_details/pic_details.js
import Dialog from '../../external/youzan_dist/dialog/dialog';
var t = require("../../../utils/DownloadHelper.js")
var request_api = require('../../../utils/require_api.js');
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    image_url: [],
    url_image: [],
    downurl: [],
    showdownbtn: false,
    maskshow: false,
    isFolded: true,
    queryBean: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    this.data.queryBean = options.item_no; //获取到页面传递的款号
    wx.setNavigationBarTitle({
      title: this.data.queryBean + '' //设置页面标题为款号
    });
    request_api.request({
        "cmd": "weixin|get_wx_style_pic",
        "paras": [{
            "name": "styleno",
            "value": this.data.queryBean
          },
          {
            "name": "ctype",
            "value": 1
          },
        ],
      }, //需要传的 data
      function(res) {
        that.data.url_image = res.data.result;
        that.setData({
          queryBean: that.data.queryBean,
          image_url: that.data.url_image
        })
      },
      function() {
        console.log("錯誤");
      },
    )
  },
  // btn_pic(event) {
  //   console.log(event);
  //   let index = event.target.dataset.index; //获取下表
  //   this.data.downurl = [];
  //   if (this.data.maskshow) { //是否在遮罩层的时候点击了图片，是的话对应的图片显示对勾，设置图片选中时候true和false
  //     const selected = this.data.url_image[index].selected;
  //     this.data.url_image[index].selected = !selected;
  //     for (let i = 0; i < this.data.url_image.length; i++) {
  //       if (this.data.url_image[i].selected) {
  //         this.data.downurl.push(this.data.url_image[i].url); //点击对应需要下载的图片存在一个数组中
  //       }
  //     }
  //   }
  //   if (this.data.check_mark) { //是否在遮罩层的时候点击了图片的对勾按钮（会显示绿色对勾）是的话就让选中的图片selected变为false
  //     const selected = this.data.url_image[index].selected;
  //     this.data.url_image[index].selected = false;
  //     for (let i = 0; i < this.data.url_image.length; i++) {
  //       if (this.data.url_image[i].selected) {
  //         this.data.downurl.push(this.data.url_image[i].url);
  //       }
  //     }
  //   }
  //   this.isdownbtnshow(this.data.downurl); //是否显示下载按钮
  //   this.data.check_mark = false;
  //   this.setData({
  //     image_url: this.data.url_image,
  //     showdownbtn: this.data.showdownbtn
  //   });
  // },
  //预览图片
  previewImage: function(e) {
    var url_image = [];
    var current = '';
    console.log(e, this.data.image_url)
    this.data.url_image = this.data.image_url;
    if (!this.data.maskshow) {
      console.log(this.data.url_image);
      for (let i = 0; i < this.data.url_image.length; i++) {
        url_image.push(this.data.url_image[i].url);
      }
      for (let i = 0; i < url_image.length; i++) {
        if (url_image[i] == e.target.dataset.pic_url.url) {
          current = url_image[i]
        }
      }
      wx.previewImage({
        current: current, // 当前显示图片的http链接  
        urls: url_image // 需要预览的图片http链接列表  
      })
    } else {}
  },
  //是否显示下载按钮，根据有没有需要下载的图片
  isdownbtnshow(url) {
    if (url.length != 0) {
      this.data.showdownbtn = true;
    } else {
      this.data.showdownbtn = false
    }
  },
  //下载图片，显示遮罩层
  downloadbtn() {
    // if (this.data.maskshow) {
    //   for (let i = 0; i < this.data.url_image.length; i++) {
    //     this.data.url_image[i].selected = false;
    //   }
    // }
    // this.setData({
    //   maskshow: !this.data.maskshow,
    //   image_url: this.data.url_image,
    //   showdownbtn: false,
    // })
    var downfileData = [];
    for (let i = 0; i < this.data.url_image.length; i++) {
      downfileData.push(this.data.url_image[i].url);
    }
    var that = this;
    setTimeout(function() {
      that.get_picturealbum(downfileData);
    }, 1500);
  },
  //点击下载区域下载内容
  get_picturealbum(downurl) {
    var that = this;
    var type = '';
    wx.getSetting({ //首先判断是否授权，没有的话就弹出授权按钮，授权成功后才能下载
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          //没有授权获取相册
          wx.showToast({
            title: '请先授权',
            duration: 2000, //显示时长
            mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
          });
          that.setData({
            openSettingBtnHidden: true //授权不成功就调用去授权的按钮
          })
          return;
        }else{
          type = 'image'; //下标为0和2是下载图片类型，1为视频类型，需要区分开
          for (let i = 0; i < downurl.length; i++) {
            downurl[i] = downurl[i].split('?')[0]
          }
          var downer = new t.DownloadHelper(type); //创建新的对象，用于下载
          downer.download(downurl).then(function () { //调用下载对象的方法，传递需要下载的地址 下面是下载成功执行的内容
            that.setData({
              showdownbtn: false, //隐藏下载按钮
              maskshow: false, //去掉遮罩层
              image_url: that.data.url_image //把对勾去掉的数组重新赋值
            })
          }).catch(function (a) {
            wx.hideLoading();
            if (a.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || a.errMsg === 'saveVideoToPhotosAlbum:fail auth denied' || a.errMsg === 'saveImageToPhotosAlbum:fail auth deny' || a.errMsg === 'saveVideoToPhotosAlbum:fail auth deny') {
              wx.showToast({
                title: '请先授权!',
                duration: 2000, //显示时长
                mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
              })
              wx.openSetting({
                success(settingdata) {
                  if (!settingdata.authSetting['scope.writePhotosAlbum']) {
                    console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                  } else {
                    console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                  }
                }
              })
              that.setData({
                openSettingBtnHidden: true //授权不成功就调用去授权的按钮
              })
            }
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})